package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public UserInfo get(Long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean checkPhone(String phone) {
        int count = userInfoMapper.checkCountByPhone(phone);
        return count > 0;
    }

    @Override
    public void sendVerifyCode(String phone) {
        // 准备验证码
        String code = UUID.randomUUID().toString().substring(0, 4);
        // 准备key
//        String key = "verify_code:" + phone;
        String key = RedisKeys.VERIFY_CODE.join(phone);
        // 存到redis
        userInfoRedisService.setVerifyCode(key, code, Consts.VERIFY_CODE_VAI_TIME); // 5分钟
        // 发送短信
        System.out.println("验证码： " + code); // TODO
    }

    @Override
    public String getVerifyCode(String phone) {
        String key = RedisKeys.VERIFY_CODE.join(phone);
        return userInfoRedisService.getVerifyCode(key);
    }

    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) throws LogicException {
        // 1、判断参数是否为空
        AssertUtil.hasLength(phone, "手机号不能为空");
        AssertUtil.hasLength(nickname, "昵称不能为空");
        AssertUtil.hasLength(password, "密码不能为空");
        AssertUtil.hasLength(rpassword, "确认密码不能为空");
        AssertUtil.hasLength(verifyCode, "验证码不能为空");
        // 2、判断 2 次输入密码是否相等
        AssertUtil.isEquals(password, rpassword, "两次密码不一致");
        // 3、判断手机号码格式是否正确
        // TODO
        // 4、判断手机号码唯一
        if(checkPhone(phone)){
            throw new LogicException("手机号码已被注册");
        }
        // 5、判断验证码是否正确
        String code = getVerifyCode(phone); // 查询存到 redis 中的验证码
        if(!verifyCode.equalsIgnoreCase(code)){
            throw new LogicException("验证码失效或验证码错误");
        }
        // 6、注册
        UserInfo userInfo = new UserInfo();
        userInfo.setPhone(phone);
        userInfo.setPassword(password);
        userInfo.setNickname(nickname);
        // 状态
        userInfo.setState(UserInfo.STATE_NORMAL);
        // 级别
        userInfo.setLevel(1);
        // 默认头像
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfoMapper.insert(userInfo);
    }

    @Override
    public String userLogin(String username, String password) {
        // 查询用户是否存在
        UserInfo userInfo = userInfoMapper.userLogin(username, password);
        if(userInfo == null){
            throw new LogicException("账号或密码错误");
        }
        // 如果不为空，把当前登录对象存到 redis 中
        // redis 的 key 必须做到唯一
        String token = UUID.randomUUID().toString().replace("-", "");
        String key = RedisKeys.TOKEN_CODE.join(token);
        String json = JSON.toJSONString(userInfo);
        userInfoRedisService.setToken(key, json, Consts.USER_INFO_TOKEN_VAI_TIME);
        return key;
    }

    @Override
    public List<UserInfo> list() {
        return userInfoMapper.selectAll();
    }
}
